package com.itheima.edu.info.manager.system.dao.impl;

import com.itheima.edu.info.manager.system.dao.ITeacherDao;
import com.itheima.edu.info.manager.system.domain.Teacher;

import java.util.ArrayList;

public class TeacherDaoImpl implements ITeacherDao {
    private static ArrayList<Teacher> list= new ArrayList<>();
    static {
        Teacher zhai = new Teacher("0001", "翟老师", 30, "1991-01-01");
        Teacher dongge = new Teacher("0002", "东哥", 30, "1991-01-01");
        Teacher liangge = new Teacher("0003", "亮哥", 30, "1991-01-01");
        Teacher li = new Teacher("0004", "李老师", 30, "1991-01-01");
        list.add(zhai);
        list.add(dongge);
        list.add(liangge);
        list.add(li);
    }


    @Override
    public void xinZeng(Teacher teacher) {
        list.add(teacher);
    }

    @Override
    public void shanChu(String id) {
        int i = this.huoQuSuoYin(id);
        list.remove(i);
    }

    @Override
    public void xiuGai(String id, Teacher teacher) {
        int i = this.huoQuSuoYin(id);
        list.set(i,teacher);
    }

    @Override
    public Teacher[] chaXunSuoYou() {
        Teacher[] teachers = new Teacher[list.size()];
        for (int i = 0; i < list.size(); i++) {
            teachers[i]=list.get(i);
        }

        return teachers;
    }

    @Override
    public int huoQuSuoYin(String id) {
        for (int i = 0; i < list.size(); i++) {
            Teacher teacher = list.get(i);
            if(teacher!=null&&teacher.getId().equals(id)){
                return i;
            }
        }
        return -1;
    }
}
